<?php

class LanguageController extends MainController implements ControllerInterface {
	
	public function IndexAction($param)
	{
		$table_data = $this->getTableData('category_table',$param,array('o' => 'name', 'v' => 'malejaco', 't' => 10));
		$v = $table_data['v'] == 'malejaco'?'DESC':'ASC';
		$oTable = array(
			'name' => "language.name",
			'display_name' => "language.display_name",
			'active' => "language.active"
		);
		$o = $oTable[$table_data['o']];
		$p = isset($param['strona'])?$param['strona']:1;
		
		$this->smarty->Assign('c', LanguageDAL::getResult(LanguageDAL::GetDalDataObj(array('count' => true))));
		
		
		//$this->smarty->assign('c',CategoryDAL::getResult(CategoryDAL::GetDalDataObj(array('count' => true))));
		//dane do tabelek i paginacji koniec - users_table


		$arrayObjLanguages = LanguageDAL::getResult(LanguageDAL::GetDalDataObj(array(
			'sortBy' => "$o $v",
			'limit' => (($p-1)*$table_data['t']).', '.$table_data['t']
		)));

		$this->smarty->assign('arrayObjLanguges', $arrayObjLanguages);		
	}
	
	public function AddAction($param)
	{
		$obj = new Language();
		$error = array();
		if(Request::GetPost('AddAction'))
		{
			$errors = $this->ValidateAdd();
			if(empty($errors))
			{
				$obj->setName(Request::GetPost('name'));
				$obj->setDisplayName(Request::GetPost('display_name'));
				$obj->setCode(Request::GetPost('code'));
				$obj->setActive(Request::GetPost('active')=='on'?1:2);
				
				$languageId = LanguageDAL::Save($obj->GetDalDataObj());
				
				return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
				
			} else {
				$error = $errors;
			}
		}
		
		$this->smarty->assign('obj', $obj);
		$this->smarty->assign('error', $error);
	}
	
	public function EditAction($param)
	{
		
		if(!isset($param['id']))
		{
			return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
		}
		
		$obj = LanguageDAL::GetResult(LanguageDAL::GetDalDataObj(array('condition' =>array( 'id'=>$param['id']))));
		
		if(!isset($obj[0]))
		{
			return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
		}
		
		
		$obj = $obj[0];
		$error = array();
		if(Request::GetPost('EditAction'))
		{
			$errors = $this->ValidateAdd();
			if(empty($errors))
			{
				$obj->setName(Request::GetPost('name'));
				$obj->setDisplayName(Request::GetPost('display_name'));
				$obj->setCode(Request::GetPost('code'));
				$obj->setActive(Request::GetPost('active')=='on'?1:0);
				$languageId = LanguageDAL::Save($obj->GetDalDataObj());
				return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
				
			} else {
				$error = $errors;
			}
		}
		
		$this->smarty->assign('obj', $obj);
		$this->smarty->assign('error', $error);		
	}
	
	public function DeleteAction($param)
	{
		if(!isset($param['id']))
		{
			return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
		}
		
		$obj = LanguageDAL::GetResult(
				LanguageDAL::GetDalDataObj(
						array(
							'condition' => array(
								'id'=>array($param['id'])
							)
						)
					)
				);
		
		if(!isset($obj[0]))
		{
			return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
		}		
		$obj = $obj[0];

		LanguageDAL::DefaultDelete($obj->GetDalDataObj());
		
		return $this->AddRedirect(Router::GenerateUrl('LanguageIndex', array('Language' => 'Index')), 0);
	}
	
	public function preDispatch($param)
	{
		$this->Run($param);
		$this->RunShared('Auth', array());		
	}
	
	public function postDispatch($param)
	{
		
	}
	
	// validacja danych
	private function ValidateAdd()
	{
		$validator = new Validator(Request::GetAllPost());

		$validator->IsEmpty('name', 'Pole "Nazwa" nie może być puste.');
		$validator->IsEmpty('display_name', 'Pole "Nazwa wyświetlana" nie może być puste.');
		$validator->IsEmpty('code', 'Pole "Kod" nie może być puste..');

		return $validator->GetErrorList();		
	}
	
	
	public function ToggleStatusAction($param) {
		$this->SetAjaxRender();

		$dalObj = LanguageDAL::GetDalDataObj(array('id' => $param['id']));
		$this->content = LanguageDAL::DefaultToggleFieldById($dalObj, 'active');
	}
	
}
?>
